package com.example.yk2021xm04.controller;


import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.Task;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.TaskQuery;
import com.example.yk2021xm04.service.ITaskService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiYongle
 * @since 2021-12-02
 */
@Controller
@RequestMapping("/task")
public class TaskController {
    @Resource
    private ITaskService taskService;

    /**
     * 任务管理主界面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "task/task";
    }

    /**
     * 添加更新任务页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateTaskPage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (null!=id) {
            model.addAttribute("task",taskService.getById(id));
        }
        return "task/add_update";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> taskList(TaskQuery taskQuery) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        return taskService.taskList(user.getId(),taskQuery);
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteTask(Integer[] ids) {
        taskService.deleteTask(ids);
        return RespBean.success("任务记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addTask(Task task) {
        taskService.addTask(task);
        return RespBean.success("任务记录添加成功");
    }


    @RequestMapping("update")
    @ResponseBody
    public RespBean updateTask(Task task) {
        taskService.updateTask(task);
        return RespBean.success("任务记录更新成功");
    }

    @RequestMapping("count")
    public String statistic() {
        return "task/task-statistics";
    }

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean taskStatistic() {
        //发布时间分组 group by day
        List<Object> registerTimeByDay = taskService.publishTimeByDay();
        //一天中的发布任务数
        List<Object> registerCountByDay = taskService.publishCountByDay();
        //日同比
        double rate = 0.0;
        //总任务数量
        Integer totalTaskCount = taskService.count();
        //今天的发布任务数量
        Integer TaskCountInDay = taskService.TaskCountInDay();
        //昨天的发布任务数量
        Integer TaskCountInYesterday = taskService.TaskCountInYesterday();

        rate = (double)(TaskCountInDay - TaskCountInYesterday) / TaskCountInYesterday;
        if(TaskCountInYesterday == 0) {
            rate = 0;
        }
        Map<String, Object> res = new HashMap<>();
        res.put("num", registerCountByDay);
        res.put("time", registerTimeByDay);
        res.put("total", totalTaskCount);
        res.put("dayCount", TaskCountInDay);
        res.put("rate", rate);
        return RespBean.success("任务统计数据返回成功",res);
    }

}
